
var DAY_IN_MILLIS = 24 * 60 * 60 * 1000;
var WEEK_IN_MILLIS = 7 * DAY_IN_MILLIS;
var MONTH_IN_MILLIS = 30 * DAY_IN_MILLIS;
var YEAR_IN_MILLIS = 365 * DAY_IN_MILLIS;


var dateHelper = {

    getCurrentTime: function() {

        var date = new Date();
        var dateString = dateHelper.formatDate(date, "yyyy-MM-dd HH:mm:ss.S");
        return dateString;
    },

    getCurrentTimeEx: function() {
        var date = new Date();
        var dateString = dateHelper.formatDate(date, "yyyy-MM-dd HH:mm:ss");
        return dateString;
    },

    getTimeByValue: function(val) {
        var date = new Date(val);
        var dateString = dateHelper.formatDate(date, "yyyy-MM-dd HH:mm:ss");
        return dateString;
    },

    getYMD: function() {
        var date = new Date();
        var dateString = dateHelper.formatDate(date, "yyyy-MM-dd");
        return dateString;
    },

    getYM: function() {
        var date = new Date();
        var dateString = dateHelper.formatDate(date, "yyyy-MM");
        return dateString;
    },

    getYMDNoFormat: function() {
        var date = new Date();
        var dateString = dateHelper.formatDate(date, "yyyyMMddHHmmssS");
        return dateString;
    },

    getPreDayYMD: function() {
        var date = new Date();
        var targetDate = new Date(date.getTime() - 24 * 60 * 60 * 1000);
        var dateString = dateHelper.formatDate(targetDate, "yyyy-MM-dd");
        return dateString;
    },

    getPreDayYMDByDay: function(str, day) {

        var date = new Date(Date.parse(str.replace(/-/g, "/")));
        var targetDate = new Date(date.getTime() - day * 24 * 60 * 60 * 1000);
        var dateString = dateHelper.formatDate(targetDate, "yyyy-MM-dd");
        return dateString;
    },

    getPreDayYMDByDayEx: function(day) {
        var date = new Date();
        var targetDate = new Date(date.getTime() - day * 24 * 60 * 60 * 1000);
        var dateString = dateHelper.formatDate(targetDate, "yyyy-MM-dd");
        return dateString;
    },

    getLastDayYMDByTime: function(str) {

        var date = new Date(Date.parse(str.replace(/-/g, "/")));
        var targetDate = new Date(date.getTime() + 24 * 60 * 60 * 1000);
        var dateString = dateHelper.formatDate(targetDate, "yyyy-MM-dd");
        return dateString;
    },

    getYMDByTime: function(time_len) {

        var date = new Date(time_len);
        var dateString = dateHelper.formatDate(date, "yyyy-MM-dd");
        return dateString;
    },

    getYMByTime: function(time_len) {

        var date = new Date(time_len);
        var dateString = dateHelper.formatDate(date, "yyyy-MM");
        return dateString;
    },

    getCurrentTimeForDisplay: function() {

        var date = new Date();
        var dateString = dateHelper.formatDate(date, "yyyy-MM-dd HH:mm:ss");
        return dateString;
    },

    getCurrentMonthFirst: function() {
        var date = new Date();
        var dateString = dateHelper.formatDate(date, "yyyy-MM");
        var firstdate = dateString + '-01';
        var month_first = dateHelper.formatDate(new Date(Date.parse(firstdate.replace(/-/g, "/"))), "yyyy-MM-dd") + " 00:00:00";

        return month_first;
    },

    getCurrentMonthLast: function() {
        var date = new Date();
        var dateString = dateHelper.formatDate(date, "yyyy-MM");
        var day = new Date(date.getYear(), date.getMonth() + 1, 0);
        var lastdate = dateString + '-' + day.getDate();
        var month_last = dateHelper.formatDate(new Date(Date.parse(lastdate.replace(/-/g, "/"))), "yyyy-MM-dd") + " 24:00:00";

        return month_last;
    },

    getLastMonthLast: function() {
        var date = new Date();
        var day = new Date(date.getFullYear(), date.getMonth(), 0).getDate();
        var enddate = new Date(new Date().getFullYear(), new Date().getMonth() - 1, day);
        var month_last = dateHelper.formatDate(enddate, "yyyy-MM-dd") + " 24:00:00";
        return month_last;
    },

    getLastMonthLastYMD: function() {
        var date = new Date();
        var day = new Date(date.getFullYear(), date.getMonth(), 0).getDate();
        var enddate = new Date(new Date().getFullYear(), new Date().getMonth() - 1, day);
        var month_last = dateHelper.formatDate(enddate, "yyyy-MM-dd")
        return month_last;
    },

    getLastMonthLastYM: function() {
        var date = new Date();
        var day = new Date(date.getFullYear(), date.getMonth(), 0).getDate();
        var enddate = new Date(new Date().getFullYear(), new Date().getMonth() - 1, day);
        var month_last = dateHelper.formatDate(enddate, "yyyy-MM")
        return month_last;
    },


    getPre2DayTime: function(date) {

        var date = new Date();
        var targetDate = new Date(date.getTime() - 2 * 24 * 60 * 60 * 1000);
        var dateString = dateHelper.formatDate(targetDate, "yyyy-MM-dd HH:mm:ss.S");
        return dateString;
    },

    formatDate: function(date, fmt) {

        var o = {
            "M+": date.getMonth() + 1, //月份 
            "d+": date.getDate(), //日 
            "h+": date.getHours() % 12 == 0 ? 12 : date.getHours() % 12, //小时 (12小时制)
            "H+": date.getHours(), //小时 (24小时制)
            "m+": date.getMinutes(), //分 
            "s+": date.getSeconds(), //秒 
            "q+": Math.floor((date.getMonth() + 3) / 3), //季度 
            "S": date.getMilliseconds() //毫秒 
        };
        if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
        for (var k in o)
            if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
        return fmt;
    },

    pattern: function(date, fmt) {
        var o = {
            "M+": date.getMonth() + 1, //月份
            "d+": date.getDate(), //日
            "h+": date.getHours() % 12 == 0 ? 12 : date.getHours() % 12, //小时 (12小时制)
            "H+": date.getHours(), //小时 (24小时制)
            "m+": date.getMinutes(), //分
            "s+": date.getSeconds(), //秒
            "q+": Math.floor((date.getMonth() + 3) / 3), //季度
            "S": date.getMilliseconds() //毫秒
        };
        var week = {
            "0": "/u65e5",
            "1": "/u4e00",
            "2": "/u4e8c",
            "3": "/u4e09",
            "4": "/u56db",
            "5": "/u4e94",
            "6": "/u516d"
        };
        if (/(y+)/.test(fmt)) {
            fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
        }
        if (/(E+)/.test(fmt)) {
            fmt = fmt.replace(RegExp.$1, ((RegExp.$1.length > 1) ? (RegExp.$1.length > 2 ? "/u661f/u671f" : "/u5468") : "") + week[date.getDay() + ""]);
        }
        for (var k in o) {
            if (new RegExp("(" + k + ")").test(fmt)) {
                fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
            }
        }
        return fmt;
    },

    getHours: function() {
        return new Date().getHours();
    },

    getTime: function() {
        return new Date().getTime();
    },

    getTimeByTime: function(str) {

        var date = new Date(Date.parse(str.replace(/-/g, "/")));
        return date.getTime();
    },

    getTimeBySecond: function() {
        return parseInt(new Date().getTime() / 1000);
    },

    getTimeBySecondByStr: function(str) {
        return parseInt(new Date(str).getTime() / 1000);
    },

    getDayExpireSecond: function() {
        var time = dateHelper.getTime();
        return time % DAY_IN_MILLIS;
    },

    truncateToDayByParam: function(_time) {

        var time = new Date(_time).getTime();
        time -= time % DAY_IN_MILLIS;

        return time;
    },

    truncateToDay: function() {
        var time = dateHelper.getTime();
        time -= time % DAY_IN_MILLIS;

        return time;
    },

    truncateToWeek: function() {
        var time = dateHelper.getTime();
        time -= time % WEEK_IN_MILLIS;

        return time;
    },

    truncateToMonth: function() {
        var time = dateHelper.getTime();
        time -= time % MONTH_IN_MILLIS;

        return time;
    },

    truncateToYear: function() {
        var time = dateHelper.getTime();
        time -= time % YEAR_IN_MILLIS;

        return time;
    },

    isBetweenTimeDuration: function(_start_time, _end_time) {
        var end_time = new Date(_end_time).getTime();
        var start_time = new Date(_start_time).getTime();
        var now = new Date().getTime();

        return (now > start_time && now < end_time) ? true : false;
    },

    getTimeByTimeString: function(time_str) {
        return new Date(time_str).getTime();
    },

    getGMTString: function() {
        return new Date().toGMTString();
    },

    getDayCount: function(year, month) {
        var count = 0;
        if (month == 2) {
            if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
                count = 29;
            } else {
                count = 28;
            }
        } else if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
            count = 31;
        } else if (month == 4 || month == 6 || month == 9 || month == 11) {
            count = 30;
        }
        return count;
    },

    //获取两天之间的天数差
    getDateDiff: function(start, end) {
        var startTime = new Date(Date.parse(start.replace(/-/g, '/'))).getTime();
        var endTime = new Date(Date.parse(end.replace(/-/g, '/'))).getTime();
        var dates = Math.abs((startTime - endTime)) / (1000 * 60 * 60 * 24);
        return dates;
    },

    setStartTime: function(time) {
        var start = new Date(time);
        start.setHours(0, 0, 0);
        return new Date(start.getTime());
    },

    setEndTime: function(time) {
        var end = new Date(time);
        end.setHours(23, 59, 59);
        return new Date(end.getTime());
    },

    getBeforeDate: function(n) {
        var n = n;
        var d = new Date();
        var year = d.getFullYear();
        var mon = d.getMonth() + 1;
        var day = d.getDate();
        if (day <= n) {
            if (mon > 1) {
                mon = mon - 1;
            } else {
                year = year - 1;
                mon = 12;
            }
        }
        d.setDate(d.getDate() - n);
        year = d.getFullYear();
        mon = d.getMonth() + 1;
        day = d.getDate();
        s = year + "-" + (mon < 10 ? ('0' + mon) : mon) + "-" + (day < 10 ? ('0' + day) : day);
        return s;
    },

    getAfterDate: function(startDate, n) {
        var n = n;
        var d = new Date(startDate);
        var year = d.getFullYear();
        var mon = d.getMonth() + 1;
        var day = d.getDate();
        if (day <= n) {
            if (mon > 1) {
                mon = mon - 1;
            } else {
                year = year - 1;
                mon = 12;
            }
        }
        d.setDate(d.getDate() + n);
        year = d.getFullYear();
        mon = d.getMonth() + 1;
        day = d.getDate();
        s = year + "-" + (mon < 10 ? ('0' + mon) : mon) + "-" + (day < 10 ? ('0' + day) : day);
        return s;
    },

}
module.exports = dateHelper;